package demo.base;

import java.util.Collection;

/**
 * 分隔器
 */
public class Join {

    public static String join(String[] array, String seperator) {
        if (array == null) {
            return null;
        }
        if (array.length == 0) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append(array[0]);
        for (int i = 1; i < array.length; i++) {
            buffer.append(seperator + array[i]);
        }
        return buffer.toString();
    }

    public static String join(String[] array, String seperator, String start, String end) {
        if (array == null) {
            return null;
        }
        if (array.length == 0) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append(start + array[0] + end);
        for (int i = 1; i < array.length; i++) {
            buffer.append(seperator + start + array[i] + end);
        }
        return buffer.toString();
    }


    public static String join(String[] strings, char c) {
        return join(strings, c + "");
    }

    public static String join(Collection<?> list, String string) {

        StringBuilder builder = new StringBuilder();

        boolean isFisrt = true;
        if (list != null) {
            for (Object element : list) {
                if (!isFisrt) {
                    builder.append(string);
                }

                builder.append(element);

                isFisrt = false;
            }
        }

        return builder.toString();
    }

    public static String join(Collection<?> list, String string, String start, String end) {

        StringBuilder builder = new StringBuilder();

        boolean isFisrt = true;
        if (list != null) {
            for (Object element : list) {
                if (!isFisrt) {
                    builder.append(string);
                }

                builder.append(start + element + end);

                isFisrt = false;
            }
        }

        return builder.toString();

    }


}
